# Create your views here.
from jobs.models import Job, Daemon

from assets.views import HttpResponseSerialized

from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, Http404, HttpResponse, HttpResponseNotFound

from admin import Daemon_stop, Daemon_start, Daemon_restart

from django.contrib.auth.decorators import permission_required

@permission_required('jobs.can_change_daemon')
def daemon(request, d_id):
  """
  Return a representation of the daemon with id == d_id.
  """
  rs = Daemon.objects.filter(id=d_id)	
  return HttpResponseSerialized(request, rs)

@permission_required('jobs.can_change_daemon')
def daemon_action(request, d_id, action):
  """
  Perform the given action on the daemon with id == d_id.
  Either stop, start or restart.
  """
  actions = {'stop':Daemon_stop, 'start':Daemon_start, 'restart':Daemon_restart}
  if not action in actions: raise Http404('No such action!')
  rs = Daemon.objects.filter(id=d_id)	
  actions[action](None, request, rs)
  rs = Daemon.objects.filter(id=d_id)	
  return HttpResponseSerialized(request, rs)
